package com.qqtech.qquan.ad.controller;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.ad.enums.AdConst;
import com.qqtech.qquan.ad.model.AdBase;
import com.qqtech.qquan.ad.model.AdBaseQuery;
import com.qqtech.qquan.ad.model.AdBaseVo;
import com.qqtech.qquan.ad.model.AdSysDict;
import com.qqtech.qquan.ad.service.AdBaseService;
import com.qqtech.qquan.ad.service.AdSysDictService;
import com.qqtech.qquan.ad.service.AdSysParamService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/ad/base")
public class AdBaseController extends BaseAdminController {
	@Resource
	private AdBaseService adBaseService;
	@Resource
	private AdSysParamService adSysParamService;
	@Resource
	private AdSysDictService adSysDictService;
	@Resource
	private CommunityBaseService communityBaseService;
	@Resource
	private MallShopService mallShopService;

	@Override
	protected BaseService<AdBase> getBaseService() {
		return adBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<AdSysDict> typeList = adSysDictService.get(AdConst.DICTYPE_AD_BASE_TYPE);
		model.addAttribute("typeList", typeList);
		return "/ad/adBase-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(AdBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		query.setStatus(YesNoEnum.YES.getCode());
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<AdBaseVo> page = adBaseService.queryPageList(query, pageKit);
		for (AdBaseVo adBaseVo : page) {
			String typeStr = adSysDictService.dictValue(AdConst.DICTYPE_AD_BASE_TYPE,
					String.valueOf(adBaseVo.getType()));
			adBaseVo.setTypeStr(typeStr);
			if(adBaseVo.getCommunityId()!=null){
				CommunityBaseVo community = communityBaseService.getById(adBaseVo.getCommunityId());
				if(community!=null){
					adBaseVo.setCommunityName(community.getName());
				}
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 创建成功
	 * 
	 * @param adBase
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result create(AdBase adBase) {
		Result ret = new Result();
		adBase.setStatus(YesNoEnum.YES.getCode());
		adBaseService.insert(adBase);
		return ret;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		List<AdSysDict> list = adSysDictService.get(AdConst.DICTYPE_AD_BASE_TYPE);
		CommunityBaseQuery query = new CommunityBaseQuery();
		query.setIsShow(YesNoEnum.YES.getCode());
		query.setIsOpen(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<CommunityBaseVo> cList = communityBaseService.queryList(query, pageKit);
		List<MallShopVo> mallShopVos = mallShopService.queryALl();
		model.addAttribute("mallShopVos", mallShopVos);
		model.addAttribute("cList", cList);
		model.addAttribute("list", list);
		return "/ad/adBase-add";
	}

	/**
	 * 失效
	 * 
	 * @param toExpireAt
	 * @return
	 */
	@RequestMapping(value = "/toExpireAt")
	@ResponseBody
	public Result toExpireAt(Integer id) {
		Result ret = new Result();
		AdBaseVo updateVo = adBaseService.getById(id);
		updateVo.setExpireAt(TimeUtil.now());
		updateVo.setStatus(YesNoEnum.NO.getCode());
		adBaseService.updateById(updateVo);
		return ret;
	}

	@RequestMapping(value = "/imgUpload", method = RequestMethod.GET)
	public String imgUpload(Model model) {
		return "/ad/adBase-imgUpload";
	}

	/**
	 * 去更新
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		AdBaseVo adBaseVo = adBaseService.getById(id);
		if (StringUtil.isNotBlank(adBaseVo.getContentImg())) {
			Iterator<String> it = Splitter.on(";").trimResults().omitEmptyStrings().split(adBaseVo.getContentImg())
					.iterator();
			List<String> listImgs = Lists.newArrayList(it);
			model.addAttribute("listImgs", listImgs);
		}
		model.addAttribute("adBaseVo", adBaseVo);
		List<AdSysDict> list = adSysDictService.get(AdConst.DICTYPE_AD_BASE_TYPE);
		model.addAttribute("list", list);
		CommunityBaseQuery query = new CommunityBaseQuery();
		query.setIsShow(YesNoEnum.YES.getCode());
		query.setIsOpen(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<CommunityBaseVo> cList = communityBaseService.queryList(query, pageKit);
		model.addAttribute("cList", cList);
		List<MallShopVo> mallShopVos = mallShopService.queryALl();
		model.addAttribute("mallShopVos", mallShopVos);
		return "/ad/adBase-update";
	}

	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(AdBase adBase) {
		Result ret = new Result();
		adBaseService.updateById(adBase);
		return ret;
	}
}
